package com.baibei.pay.recharge;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.module.AppRouter;
import com.baibei.pay.R;
import com.baibei.pay.R2;
import com.bumptech.glide.Glide;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebPayActivity extends AppCompatActivity {

    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.iv_QR)
    ImageView ivQR;
    @BindView(R2.id.tv_pay)
    TextView tvPay;
    @BindView(R2.id.tv_finish)
    TextView tvFinish;
    @BindView(R2.id.wv)
    WebView wv;
    private String codeUrl;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        String codeImgUrl = intent.getStringExtra("codeImgUrl");
        codeUrl = intent.getStringExtra("codeUrl");
        Log.i("rechargehhh", codeUrl);
        tvTitle.setText(type == 1 ? "支付宝扫码支付" : "微信支付");
        Glide.with(this).load(codeImgUrl).into(ivQR);
    }

    @OnClick({R2.id.tv_pay, R2.id.tv_finish})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_pay) {
//            Intent intent = new Intent(WebPayActivity.this, AlipayWebviewActivity.class);
//            intent.putExtra("codeUrl",codeUrl);
//            startActivity(intent);
            if (type == 2) {
                Map<String, String> map = new HashMap<>();
                map.put("Referer", "http://www.100bei.com");
                WebSettings settings = wv.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setDefaultTextEncodingName("UTF-8");
                wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                wv.setWebChromeClient(new WebChromeClient());
                WebViewClient webViewClient = new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // 如下方案可在非微信内部WebView的H5页面中调出微信支付
                        if (url.startsWith("weixin://wap/pay?")) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(url));
                            startActivity(intent);
                            return true;
                        } else {
                            Map<String, String> extraHeaders = new HashMap<String, String>();
                            extraHeaders.put("Referer", "http://wxpay.wxutil.com");
                            view.loadUrl(url, extraHeaders);
                        }
                        return true;
                    }


                    // 重写此方法可以让webview处理https请求
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
                        handler.proceed();
                    }
                };
                wv.setWebViewClient(webViewClient);
                wv.loadUrl(codeUrl, map);
                return;
            }
            if (type == 1) {
                Intent intent = new Intent();
                Log.e("TAG", "onViewClicked: "+codeUrl );
                if (codeUrl.contains("HTTP")){
                    codeUrl = "http://" + codeUrl.split("HTTP://")[1];
                }
                intent.setData(Uri.parse(codeUrl));
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);
            }
        } else {
            toUserCenter();
        }
    }

    private void toUserCenter() {
        AppRouter.routeToMain(this);
//        ARouter.getInstance().build(AppRouter.ROUTER_MAIN)
//                .navigation();
//        AppRouter.routeToMain(this,3);
//        Bundle bundle = new Bundle();
//        bundle.putInt("code",3);
//        ARouter.getInstance().build("/ebec/home")
//                .withBundle("bundle",bundle)
//                .navigation();
//        ARouter.getInstance().build(AppRouter.ROUTER_MAIN)
//                .withInt("flag",3)
//                .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
//                .navigation();
//        finish();
    }
}
